import os  
  
def file_name(file_dir):   
    for root, dirs, files in os.walk(file_dir):  
        print(root) #当前目录路径  
        print(dirs) #当前路径下所有子目录  
        print(files) #当前路径下所有非目录子文件  


if __name__ == "__main__":
    for root, dirs, files in os.walk("./blktrace"):  
        all_file_name = files #当前路径下所有非目录子文件  
        break

    print(all_file_name)

    read_file_number = 0
    write_file_number = 0
    for file_name in all_file_name:
        trace_fd = open("blktrace/"+file_name,"r")
        for line in trace_fd:
            line_split = line.split(" ")
            line_split = list(filter(None, line_split))  # 只能过滤空字符和None
            #print(line_split)

            if '7' in line_split[0] and len(line_split)>8:
                pid = line_split[4]
                lpn = int(line_split[7])
                read_count = int(line_split[9])
                r_w = "read"
                if 'W' in line_split[6]:
                    r_w = "write"

                #读取信息完毕，开始写入trace文件
                fd = None
                if r_w == "read":
                    fd = open("seperated_blktrace/"+r_w+"/"+ str(read_file_number) +".log","w")
                    read_file_number += 1 
                else:
                    fd = open("seperated_blktrace/"+r_w+"/"+ str(write_file_number) +".log","w")
                    write_file_number += 1

                #fd.write(r_w+"\n")

                for i in range(read_count):
                    fd.write(str(lpn+i)+"\n")

                fd.close()

            else:
                break